package com.dhnsoft.数据结构与算法.链表;

/**
 * ClassName: DoubleLinkedListDemo
 * Package: com.dhnsoft.数据结构与算法.链表
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/6 9:52
 * @Version 1.0
 */
public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        HeroNode2 h1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 h2 = new HeroNode2(3,"卢俊义","玉麒麟");
        HeroNode2 h3 = new HeroNode2(5,"吴用","智多星");
        HeroNode2 h4 = new HeroNode2(7,"林冲","豹子头");

        DoubleLinkedList d = new DoubleLinkedList();
        d.addNode(h1);
        d.addNode(h2);
        d.addNode(h3);
        d.addNode(h4);
        d.list();

        d.delele(1);
        d.list();
    }
}

class DoubleLinkedList{
    HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    //双向链表删除一个节点
    public void delele(int no){
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (temp!=null){
            if(temp.no==no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.pre.next = temp.next;
            if(temp.next!=null){
                temp.next.pre =temp.pre;
            }

        }
        else{
            System.out.printf("没有找到no：%d的节点\n",no);
        }

    }

    //双向链表中增加一个节点
    public void addNode(HeroNode2 node){
        HeroNode2 temp = head;
        while (temp.next!=null){
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }

    //修改节点代码
    public void update(HeroNode2 heroNode){
        //判断是否为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head;
        boolean flag = false;
        while(true){
            if(temp==null){
                break;
            }
            if(heroNode.no==temp.no){
                flag = true;
                break;
            }
            temp = temp.next;

        }
        if(flag){
            temp.nickName=heroNode.nickName;
            temp.name=heroNode.name;
        }
        else{
            System.out.printf("没有找到编号%d的节点，不能修改\n",heroNode.no);
        }
    }

    //显示链表（遍历）
    public void list(){
        //判断链表是否为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head;
        while (true){
            if(temp==null){
                break;
            }
            System.out.println(temp);
            //链表一定得后移
            temp = temp.next;
        }
    }
}

//定义一个HeroNode
class HeroNode2{
    int no;
    String name;
    String nickName;
    HeroNode2 next; //指向下一个节点
    HeroNode2 pre; //指向前一个节点

    public HeroNode2(int no,String name,String nickName){
        this.name = name;
        this.no = no;
        this.nickName = nickName;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                "" +
                '}';
    }
}

